package com.example.demo1.smconfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import java.util.EnumSet;

/**
 * 状态机配置类
 */
@Configuration
@EnableStateMachine
//@EnableStateMachine(name = "turnstileStateMachine")
public class TurnstileStateMachineConfig extends EnumStateMachineConfigurerAdapter<TurnstileStates, TurnstileEvents> {

    /**
     * 配置状态
     * 初始化状态为LOCKED，设置了所有的状态
     * @param states
     * @throws Exception
     */
    @Override
    public void configure(StateMachineStateConfigurer<TurnstileStates, TurnstileEvents> states) throws Exception {
        states.withStates()
                .initial(TurnstileStates.LOCKED)
                .states(EnumSet.allOf(TurnstileStates.class));
        /*states.withStates()
                .initial(TurnstileStates.LOCKED)  // 设置初始状态
                .state(TurnstileStates.UNLOCKED);*/
    }


    /**
     * 配置状态转换
     * 将业务分解为 几个切片，配置每种情况下的转换关系
     * @param transitions
     * @throws Exception
     */
    @Override
    public void configure(StateMachineTransitionConfigurer<TurnstileStates, TurnstileEvents> transitions)
            throws Exception {
        transitions.withExternal()
                .source(TurnstileStates.LOCKED).target(TurnstileStates.UNLOCKED)
                .event(TurnstileEvents.COIN).action(onEnterUnlockStateByCoin()) //投币/支付事件 --> 状态由 LOCKED 转为 UNLOCKED
                .and()
                .withExternal()
                .source(TurnstileStates.UNLOCKED).target(TurnstileStates.LOCKED)
                .event(TurnstileEvents.PUSH).action(onEnterLockStateBycustomerPass())
        ; //按下按钮/触发事件 --> 状态由 UNLOCKED 转为 LOCKED
    }

    @Override
    public void configure(StateMachineConfigurationConfigurer<TurnstileStates, TurnstileEvents> config) throws Exception {
        config.withConfiguration()
                .autoStartup(true);  // 自动启动
    }

    @Bean
    public TurnstileStates[] turnstileStates() {
        return TurnstileStates.values();
    }

    @Bean
    public TurnstileEvents[] turnstileEvents() {
        return TurnstileEvents.values();
    }

    /**
     * 进入 unlock 状态时，执行的动作
     * @return
     */
    public Action<TurnstileStates, TurnstileEvents> onEnterUnlockStateByCoin() {
        return context -> System.out.println("进入状态action::解锁旋转门，以便游客能够通过" );
    }

    /**
     * 进入 unlock 状态时，执行的动作
     * @return
     */
    public Action<TurnstileStates, TurnstileEvents> onEnterLockStateBycustomerPass() {
        return context -> System.out.println("进入状态action::当游客通过，锁定旋转门" );
    }

}